//
//  BK_OD_Footer_PromptMsg.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/3/3.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_OD_Footer_PromptMsg.h"
#import "BK_OD_Color.h"
/**
 限购提示
 */
@implementation BK_OD_Footer_PromptMsg
{
    UILabel *_messageL;
    UIButton *_details;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.backgroundColor = [UIColor clearColor];
    
    _messageL = [[UILabel alloc] init];
    _messageL.textColor = kOrderLightGrey1;
    _messageL.font = UIFontCustom(12);
    [self addSubview:_messageL];
    
    _details = [UIButton buttonWithType:UIButtonTypeCustom];
    _details.titleLabel.font = UIFontCustom(12);
    [_details addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_details];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    /*
     * 描述
     */
//    [_messageL mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self).offset(ApplicationBackGauge);
//        make.top.mas_equalTo(self).offset(5);
//    }];
    [_messageL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(ApplicationBackGauge);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    /*
     * 限购详情
     */
//    [_details mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(_messageL.mas_right).offset(ApplicationBackGauge);
//        make.centerY.mas_equalTo(_messageL);
//    }];
    
    [_details mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_messageL.mas_right).offset(ApplicationBackGauge);
        make.centerY.mas_equalTo(_messageL);
    }];
    
    
}

- (void)showDetails:(UIButton *)sender
{
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(BKODFooterPromptMsgDelegate)]) {
        [self.delegate ODFooterPromptMsgPromptMsgShowDetails:self];
    }
}
- (void)setMessage:(NSString *)message
{
    _message = message;
    _messageL.attributedText = [self creatLableAttribuedString:message];
    /*
     * 限购详情
     */
    NSAttributedString *attString = [self addUnderline:kOrderLocalizeString(@"BKOrderDDLimitBuyDetails")];
    [_details setAttributedTitle:attString forState:UIControlStateNormal];
}

- (NSAttributedString *)addUnderline:(NSString *)string
{
    UIColor *textDefaultColor = _details.tintColor;
    NSDictionary *paramDic = @{
                               NSUnderlineStyleAttributeName     : @(NSUnderlineStyleSingle),
                               NSUnderlineColorAttributeName     : textDefaultColor,
                               NSForegroundColorAttributeName    : textDefaultColor
                               };
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string attributes:paramDic];
    
    return attString.copy;
}

- (NSMutableAttributedString *)creatLableAttribuedString:(NSString *)string{
    if (string&&string.length>0) {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
        
        //红色个数最多为2位数(如果是数字则为红色)
        NSInteger shouldRedStrLength = 2;
        NSString *shouldRedStr;
        do{
            if (shouldRedStrLength <= 0) {
                return attributedString;
            }
            shouldRedStr = [string substringWithRange:NSMakeRange(string.length - (shouldRedStrLength + 1), shouldRedStrLength)];
            shouldRedStrLength -= 1;
        }
        while (![self isPureInt:shouldRedStr]);
        
        [attributedString addAttribute:NSForegroundColorAttributeName value:kOrderPriceRed range:NSMakeRange(string.length - (shouldRedStrLength + 1), shouldRedStrLength)];
        return attributedString;
    }
    return nil;
}

- (BOOL)isPureInt:(NSString *)string
{
    NSScanner *scanner = [NSScanner scannerWithString:string];
    
    int val;
    
    return [scanner scanInt:&val] && [scanner isAtEnd];
}

@end

